<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>重置管理员密码</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 10px;
        }
        .button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            white-space: pre-wrap;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔧 管理员密码重置工具</h1>

        <div class="info">
            <strong>说明：</strong>
            <br>• 此工具用于重置admin用户的密码为 admin123
            <br>• 请确保后端服务已启动（端口8765）
            <br>• 点击按钮后会自动更新数据库中的密码哈希
        </div>

        <button class="button" onclick="resetPassword()">🔄 重置admin密码为 admin123</button>
        <button class="button" onclick="testPassword()">🧪 测试密码验证</button>
        <button class="button" onclick="generateHash()">🔐 生成新密码哈希</button>

        <div id="result"></div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8765/api';

        async function resetPassword() {
            showResult('正在重置密码...', 'info');

            try {
                const response = await fetch(`${API_BASE}/debug/password/reset-admin`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });

                const data = await response.json();

                if (data.code === 200) {
                    showResult(`✅ 密码重置成功！\n\n${data.data}\n\n现在可以使用 admin/admin123 登录了。`, 'success');
                } else {
                    showResult(`❌ 重置失败：${data.message}`, 'error');
                }
            } catch (error) {
                showResult(`❌ 网络错误：${error.message}\n\n请检查后端服务是否启动（端口8765）`, 'error');
            }
        }

        async function testPassword() {
            const password = prompt('请输入要测试的密码:', 'admin123');
            if (!password) return;

            const hash = prompt('请输入密码哈希:', '$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi');
            if (!hash) return;

            try {
                const response = await fetch(`${API_BASE}/debug/password/verify?password=${encodeURIComponent(password)}&hash=${encodeURIComponent(hash)}`, {
                    method: 'POST'
                });

                const data = await response.json();
                showResult(`🧪 密码验证结果：\n\n密码: ${password}\n哈希: ${hash}\n验证结果: ${data.data}`, 'info');
            } catch (error) {
                showResult(`❌ 验证失败：${error.message}`, 'error');
            }
        }

        async function generateHash() {
            const password = prompt('请输入要生成哈希的密码:', 'admin123');
            if (!password) return;

            try {
                const response = await fetch(`${API_BASE}/debug/password/encode?password=${encodeURIComponent(password)}`);

                const data = await response.json();
                showResult(`🔐 新的密码哈希：\n\n密码: ${password}\n哈希: ${data.data}\n\n可以将此哈希更新到数据库中。`, 'success');
            } catch (error) {
                showResult(`❌ 生成失败：${error.message}`, 'error');
            }
        }

        function showResult(message, type) {
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = message;
            resultDiv.className = `result ${type}`;
        }
    </script>
</body>
</html>